/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void SWD_Pin_To_PB5_PD5_Configuration(void);
static void Delay(__IO uint32_t nCount);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */

  Delay(0xFFFFF);

  SWD_Pin_To_PB5_PD5_Configuration();

  while(1)
  {
    GPIOB->ODR |= (0x1 << 5);
    Delay(0xFF);
    GPIOD->ODR &= ~(0x1 << 5);
    Delay(0xFF);

    GPIOB->ODR &= ~(0x01 << 5);
    Delay(0xFF);
    GPIOD->ODR |= (0x01 << 5);
    Delay(0xFF);
  }
}

/**
  * @brief  Configure SWDIO pin to GPIO function
  * @param  None
  * @retval None
  */
void SWD_Pin_To_PB5_PD5_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable IOMUX clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_IOMUX, ENABLE);

  /* Enable GPIOB adn GPIOD clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);

  /* Configure SWDIO to PD5 */
#if defined (HK32F0301MJ4M7C) /* for SOP8 - PIN8*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN8, IOMUX_PD5_SEL_PD5);
#elif defined (HK32F0301MD4P7C) /*for TSSOP16 - PIN 15*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN15, IOMUX_PD5_SEL_PD5);
#elif defined (HK32G003F4P7) /* for TSSOP20 - PIN 2 */
  GPIO_IOMUX_ChangePin(IOMUX_PIN2, IOMUX_PD5_SEL_PD5);
#elif defined (HK32F0301MF4N7C)  /* for QFN20 - PIN19*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN19, IOMUX_PD5_SEL_PD5);
#endif

  /* Configure SWCLK to PB5 */
#if defined (HK32F0301MJ4M7C) /* for SOP8 - PIN5*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN5, IOMUX_PB5_SEL_PB5);
#elif defined (HK32F0301MD4P7C) /*for TSSOP16 - PIN 9*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN9, IOMUX_PB5_SEL_PB5);
#elif defined (HK32G003F4P7) /* for TSSOP20 - PIN 11 */
  GPIO_IOMUX_ChangePin(IOMUX_PIN11, IOMUX_PB5_SEL_PB5);
#elif defined (HK32F0301MF4N7C)  /* for QFN20 - PIN8*/
  GPIO_IOMUX_ChangePin(IOMUX_PIN8, IOMUX_PB5_SEL_PB5);
#endif

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

/**
  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */
static void Delay(__IO uint32_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


